<?php
declare (strict_types = 1);

namespace app\controller\api\v1\user\course;

use app\model\communal\CourseModel;
use app\service\logic\common\course\CourseService;
use app\service\logic\common\order\OrderCourseService;
use app\service\logic\common\user\CourseCollectService;
use app\validate\common\course\CourseValidate;
use think\App;
use think\Request;
use think\Response;
use think\response\Json;
use think\db\exception\DbException;
use app\controller\backend\BackendController;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class CourseController extends BackendController
{
    protected $service;

    public function __construct(App $app, CourseService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);
        $this->validate($params, CourseValidate::class . '.index');

        $fun = function ($model) {
            // 类型 1 直播课程 2 点播课程
            $typeMap = [1 => '直播课程', 2 => '点播课程'];
            $model['type_map'] = $typeMap[$model['type']];
            unset($model['belongCategory'], $model['byCreator']);
            return $model;
        };
        $result = $this->service->getList($params, $search_fields, $search, ['belongCategory', 'byCreator'], $fun);
        return amis($result);
    }

    /**
     * 下拉菜单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $drop = $this->service->getOptions($params, $search_fields, $search);
        return amis(['options' => $drop]);
    }

    /**
     * 显示资源
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read()
    {
        $params = $this->request->param();
        $this->validate($params, CourseValidate::class . '.read');
        $user = $this->request->middleware('user');

        $fun = function ($model) use ($user) {
            // 是否被当前用户关注
            $model['is_follow'] = invoke(CourseCollectService::class)->isCollect($user['id'], $model['id']);

            // 节数量
            $model['sections_count'] = count($model['manySections']);
            return $model;
        };
        // 是否已收藏
        $result = $this->service->read($params, ['manySections'], $fun);
        return amis($result);
    }

    /**
     * 课程类型选项
     *
     * @return Json
     */
    public function type()
    {
        $options = $this->service->amisOptions(CourseModel::$typeOptions, 'name', 'id', '全部');
        return amis(['options' => $options]);
    }

    /**
     * @return Json
     */
    public function order()
    {
        $options = $this->service->amisOptions(CourseModel::$orderOptions);
        return amis(['options' => $options]);
    }

    /**
     * 筛选 测试价格选项
     *
     * @return Json
     */
    public function price()
    {
        $options = $this->service->amisOptions(CourseModel::$priceOptions, 'name', 'id', '全部');
        return amis(['options' => $options]);
    }

    /**
     * @return Json
     */
    public function payment()
    {
        $params = $this->request->only(['id', 'payment_type', 'is_coin']);
        $this->validate($params, CourseValidate::class . '.payment');
        $user = $this->request->middleware('user');
        $params['user_id'] = $user['id'];

        // 创建待支付订单
        $order = invoke(OrderCourseService::class)->create($params);
        // 支付订单
        $payment = invoke(OrderCourseService::class)->payment($order);
        // $payment = invoke(OrderCourseService::class)->paymentPage($order); // alipay debug用
        return amis([
            'order' => $order,
            'payment' => $payment,
        ]);
    }
}
